public class test3 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        List<String> res;
        List<TreeNode> paths;
        public List<String> binaryTreePaths(TreeNode root) {
            res = new ArrayList<>();
            paths = new ArrayList<>();
            getPaths(root);
            return res;
        }
        private void getPaths(TreeNode root){
            if(root==null){
                return;
            }
            //前序
            paths.add(root);
            if(root.left==null && root.right==null){
                StringBuilder sb = new StringBuilder();
                for(int i = 0;i<paths.size()-1;i++){
                    sb.append(paths.get(i).val+"->");
                }
                sb.append(paths.get(paths.size()-1).val);
                res.add(sb.toString());
            }
            if(root.left!=null){
                getPaths(root.left);
                paths.remove(paths.size()-1);
            }
            if(root.right!=null){
                getPaths(root.right);
                paths.remove(paths.size()-1);
            }
        }
}
